<?php

namespace catchAdmin\smstopic\controller;

use catcher\base\CatchRequest;
use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\smstopic\model\SmsTopic as SmsTopicModel;
use think\Response;

class SmsTopic extends CatchController
{

    protected $smsTopicModel;

    /**
     *
     * @time 2022/10/08 11:22
     * @param SmsTopicModel $smsTopicModel
     * @return mixed
     */
    public function __construct(SmsTopicModel $smsTopicModel)
    {
        $this->smsTopicModel = $smsTopicModel;
    }

    /**
     *
     * @time 2022/10/08 11:22
     * @return Response
     */
    public function index(): Response
    {
        return CatchResponse::paginate($this->smsTopicModel->getList());
    }

    /**
     *
     * @time 2022/10/08 11:22
     * @param Request $request
     * @return Response
     */
    public function save(Request $request): Response
    {
        return CatchResponse::success($this->smsTopicModel->storeBy($request->post()));
    }

    /**
     *
     * @time 2022/10/08 11:22
     * @param $id
     * @return Response
     */
    public function read($id): Response
    {
        return CatchResponse::success($this->smsTopicModel->findBy($id));
    }

    /**
     *
     * @time 2022/10/08 11:22
     * @param $id
     * @param Request $request
     * @return Response
     */
    public function update($id, Request $request): Response
    {
        return CatchResponse::success($this->smsTopicModel->updateBy($id, $request->post()));
    }

    /**
     *
     * @time 2022/10/08 11:22
     * @param $id
     * @return Response
     */
    public function delete($id): Response
    {
        return CatchResponse::success($this->smsTopicModel->deleteBy($id));
    }

    /**
     * 获取问卷列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     */
    public function getTopicTree(CatchRequest $request): \think\response\Json
    {
        $isAll = $request->param("is_all", 0, "int");
        if ($isAll) {
            $result = $this->smsTopicModel->with("children")->select();
        } else {
            $model = [];
            $modelName = $request->param('model_name', []);

            if ($modelName) {
                $model = $modelName;
            }
            $result = $this->smsTopicModel->where("model", "in", $model)->with("children")->select();
        }
        return CatchResponse::success($result);
    }


}